$(function () {
    // 搜索下拉显示及隐藏 并改变搜索框样式（即添加active类名）
    $('.btos-search .search-drop input').focus(function () {
        $('.search-drop .drop').show();
        $('.btos-search').addClass('active');
    });

    // 下拉框点击消失
    $('body').on('click', function (e) {
        if (!$('.searchInput').is(e.target)) {
            $('.search-drop .drop').hide();
            $('.btos-search').removeClass('active');
        }
    });

    $('.btos-search .classify-drop').mouseover(function () {
        $('.btos-search').addClass('active');
    }).mouseleave(function () {
        $('.btos-search').removeClass('active');
    });
    // end
    // 视频播放
    $('.videoshow .video').hover(function () {
        $(this).find('img').hide();
        $(this).find('video').get(0).play();
    }, function () {
        $(this).find("img").show();
        $(this).find("video").get(0).pause();
    })
    
    // 头部吸顶悬浮
    $(document).on('scroll', function () {
        fixed();
    })

    function fixed() {
        var scrollTop = $(document).scrollTop();
        if (scrollTop > 60) {
            $('.btos-header').addClass('active');
        } else {
            $('.btos-header').removeClass('active');
        }
    }
    // 右侧悬浮置顶
    $('.btos-flright .top').on('click', function () {
        returnTop(500);
    })

    function returnTop(h) {
        $("html,body").animate({
            scrollTop: 0
        }, h)
    }


    $('.index_info_s').click(function () {
        window.location.href = '/user/info';
    });

    $('.index_info_d').click(function () {
        window.location.href = '/user/infoRecord?type=1&typeId=3';
    });

    //企业客服
    $('.qy_udesk').click(function () {
        (function (a, h, c, b, f, g) {
            a["UdeskApiObject"] = f;
            a[f] = a[f] || function () {
                (a[f].d = a[f].d || []).push(arguments)
            };
            g = h.createElement(c);
            g.async = 1;
            g.charset = "utf-8";
            g.src = b;
            c = h.getElementsByTagName(c)[0];
            c.parentNode.insertBefore(g, c)
        })(window, document, "script", "https://assets-cli.udesk.cn/im_client/js/udeskApi.js", "ud");
        ud({
            "code": "1kdi269f",
            "link": "https://baotuwang.udesk.cn/im_client/?web_plugin_id=49320&group_id=75652&language=zh-cn",
            'css': {
                display: "none"
            }
        });

        $("#udesk_btn").find('a').click();
    });

    //个人客服
    $(".udesk_kefu").click(function () {
        (function (a, h, c, b, f, g) {
            a["UdeskApiObject"] = f;
            a[f] = a[f] || function () {
                (a[f].d = a[f].d || []).push(arguments)
            };
            g = h.createElement(c);
            g.async = 1;
            g.charset = "utf-8";
            g.src = b;
            c = h.getElementsByTagName(c)[0];
            c.parentNode.insertBefore(g, c)
        })(window, document, "script", "//assets-cli.udesk.cn/im_client/js/udeskApi.js", "ud");
        ud({
            "code": "1kdi269f",
            "link": "//baotuwang.udesk.cn/im_client/?web_plugin_id=50478&language=zh-cn",
            'css': {
                display: 'none'
            },
        });

        $("#udesk_btn").find('a').click();
    });

    //分类页和搜索页展开更多标签的按钮
    if ($('.classify-labels .more-btn').length > 0) {
        var moreBtn = $('.classify-labels .more-btn');
        var classifyWrap = $('.classify-labels .right-wrap');

        moreBtn.each(function (i, ele) {
            $this = $(this);
            var wrap = $this.siblings('.right-wrap');
            //44为一行的高度
            if (wrap.height() != 44) {
                $this.show();
            } else {
                $this.hide();
            }
        })

        if ($('.expandTab').length > 0) {
            moreBtn.each(function (i, ele) {
                ele.onOff = true;
            });
            moreBtn.find('.more').text('收起');
        } else {
            moreBtn.each(function (i, ele) {
                ele.onOff = false; //false为收起状态
            });
            classifyWrap.css('height', 44);
        }

        moreBtn.click(function () {
            var classifyWrap = $(this).prev('.right-wrap');
            var more = $(this).find('.more');
            if (this.onOff) {
                classifyWrap.css('height', 44);
                more.text('更多')
                $(this).removeClass('active');
            } else {
                classifyWrap.get(0).style.cssText = '';
                more.text('收起')
                $(this).addClass('active');
            }
            this.onOff = !this.onOff;
        });
    }
    // 音频播放
    var $playBtn = $('.playbtn'),
        $waveLength = parseInt($('.audio-box .time-bar').css('width')),
        oldPlayItem = null,
        hasOtherPlay = false,
        isDetailPage = false;

    window.onresize = function () {
        $waveLength = parseInt($('.audio-box .time-bar').css('width'));
        audiosInit();
    }

    var timeTip = document.createElement('p');
    timeTip.className = 'timeTip';

    audiosInit();
    function audiosInit() {
        $playBtn.each(function (i, ele) {
            var that = this,
                $this = $(this);
            this.isPlay = true;
            this.showTime = 0;
            this.first = true;
            this.$thisAudio = $this.parent().find('audio');
            this.audio = this.$thisAudio.get(0);

            this.$toTime = this.$thisAudio.data("time");
            // this.$toTime = Math.ceil(this.audio.duration);
            this.pgBar = $this.siblings('.time-bar').get(0);
            this.pgBar.pgBarClientLeft = this.pgBar.getBoundingClientRect().left;
            this.pgBar.audio = this.audio;
            this.pgBar.$toTime = this.$toTime;
            this.pgBar.timeTip = timeTip;
            this.pgBar.audioBox = $this.parent().get(0);
            // this.pgBar.toParLeft = $(this.pgBar).position().left;
            this.pgBar.toParLeft = this.pgBar.pgBarClientLeft - this.pgBar.audioBox.getBoundingClientRect().left;
            // console.log('this.pgBar.toParLeft=',this.pgBar.toParLeft);
            that.$curTime = $this.parent().find('.star-time');
            that.$moveColor = $this.parent().find('.time-bar').find('.move-color');
            that.$rotateImg = $this.parent().parent().find('.souse-img');

            if ($this.parents('.work-info-wrap').length > 0) {
                $waveLength = parseInt($(this.pgBar).css('width'));
            }
            if ($this.parents('.detail-style').length > 0) {
                $waveLength = parseInt($(this.pgBar).css('width'));
            }
            //console.log($waveLength)
            that.t = TweenMax.to(that.$moveColor, that.$toTime, {
                width: $waveLength,
                ease: Linear.easeNone,
                onComplete: function () {
                    that.$moveColor.css('width', 0);
                }
            });
            that.t.pause();
            that.audio.onended = function () {
                clearInterval(that.$audioInterval);
                that.t.seek(that.$toTime);
                that.$moveColor.css('width', 0);
                that.$curTime.text('0:00');
                $(that).removeClass('pausebtn');
                that.$rotateImg.removeClass('running');
                that.isPlay = true;
                hasOtherPlay = false;
                that.first = true;
            }
            //修改进度
            this.pgBar.onmouseover = function (e) {
                this.audioBox.appendChild(countTips(e, this));
                this.onmousemove = function (e) {
                    countTips(e, this);
                };
                this.onmouseout = function () {
                    this.audioBox.removeChild(this.timeTip);
                }
                this.onclick = function (e) {
                    var willTo = Math.round((e.clientX - this.pgBarClientLeft) / $waveLength * this.$toTime);
                    that.t.seek(willTo);
                    this.audio.currentTime = willTo;
                    if (this.audio.paused) {
                        that.t.resume();
                        this.audio.play();
                        doPlay(that);
                        window.onblur = function () {
                            doPause(that);
                            that.isPlay = true;
                        }
                        that.isPlay = !that.isPlay;
                    }
                };

                function countTips(e, that) {
                    var $that = $(that);
                    if ($that.parents('.work-info-wrap').length > 0) {
                        $waveLength = parseInt($that.css('width'));
                    }
                    if ($that.parents('.detail-style').length > 0) {
                        $waveLength = parseInt($that.css('width'));
                    }
                    var willTo = Math.round((e.clientX - that.pgBarClientLeft) / $waveLength * that.$toTime);
                    var minute = willTo / 60;
                    var second = willTo % 60;
                    if (willTo <= 9) {
                        that.timeTip.innerHTML = parseInt('0' + minute) + ":" + '0' + second;
                    } else {
                        that.timeTip.innerHTML = parseInt('0' + minute) + ":" + second;
                    }
                    that.timeTip.style.left = e.clientX - that.pgBarClientLeft + that.toParLeft - 20 + 'px';
                    // console.log(e.clientX,that.pgBarClientLeft,that.toParLeft);
                    return that.timeTip;
                }
            }
            function doPlay(that) {
                if (hasOtherPlay || oldPlayItem != that) {
                    if (oldPlayItem) {
                        var $old = $(oldPlayItem);
                        clearInterval(oldPlayItem.$audioInterval);
                        oldPlayItem.$rotateImg.removeClass('running');
                        oldPlayItem.$thisAudio.get(0).pause();
                        oldPlayItem.t.pause();
                        $old.removeClass('pausebtn');
                        oldPlayItem.isPlay = true;
                    }
                }
                that.$rotateImg.addClass('running');
                $this.addClass('pausebtn');
                that.$audioInterval = setInterval(function () {
                    fun(that);
                }, 16);
                hasOtherPlay = true;
                oldPlayItem = that;
            }
            function fun(audio) {
                audio.showTime = Math.floor(audio.$thisAudio.get(0).currentTime);
                var minute = Math.floor(audio.showTime / 60);
                var second = Math.round(audio.showTime % 60);
                if (minute <= 9) {
                    minute = '0' + minute;
                }
                if (second <= 9) {
                    second = '0' + second;
                }

                audio.$curTime.text(minute + ":" + second);
            }
            function doPause(that) {
                clearInterval(that.$audioInterval);
                that.$rotateImg.removeClass('running');
                that.pauseTime = that.audio.currentTime;
                that.audio.pause();
                that.t.pause();
                $this.removeClass('pausebtn');
                hasOtherPlay = false;
            }
            //播放暂停
            $(this).click(function () {
                var $this = $(this),
                    that = this;
                if (this.isPlay) {
                    if (that.first) {
                        if (that.t.progress() == 1) {
                            that.t.progress(0);
                        }
                        that.t.resume();

                        window.onblur = function () {
                            doPause(that);
                            that.isPlay = true;
                        }

                        if (that.first && that.$toTime < 5) {
                            setTimeout(function () {
                                that.audio.play();
                            }, 300);
                        } else {
                            that.audio.play();
                        }
                        that.first = false;
                    } else {
                        that.t.resume(that.pauseTime);
                        that.audio.play();
                    }
                    doPlay(that);
                } else {
                    doPause(that);
                }
                this.isPlay = !this.isPlay;
                // 暂停

            })
        });
    }

})